﻿using System;
using System.Linq.Expressions;
using Object = UnityEngine.Object;

namespace Vasuleg.UIDrive
{
    public class UIDriveNodeBuilder<T>
    {
        private readonly Object _target;
        private readonly Action<T> _func;
        private readonly UIDrive _uiDrive;

        internal UIDriveNodeBuilder(Object target, Action<T> func, UIDrive uiDrive)
        {
            _target = target;
            _func = func;
            _uiDrive = uiDrive;
        }


        /// <summary>
        /// 绑定属性表达式
        /// </summary>
        /// <param name="eval"></param>
        /// <returns></returns>
        public UIDriveNode<T> For(Expression<Func<T>> eval)
        {
            return CommonBuild(new UIDriveCommonNode<T>(_uiDrive, _func, eval));
        }

        /// <summary>
        /// 计算属性，Computed和For只能两者择一，混用会有
        /// </summary>
        /// <param name="computed"></param>
        /// <returns></returns>
        public UIDriveNode<T> Computed(Func<T> computed)
        {
            return CommonBuild(new UIDriveComputedNode<T>(_uiDrive, _func, computed));
        }

        private UIDriveNode<T> CommonBuild(UIDriveNode<T> uiDriveNode)
        {
            uiDriveNode.Target = _target;
            _uiDrive.AddUIDriveNode(uiDriveNode);
            return uiDriveNode;
        }
    }
}